home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 476-500 / disk_500 / wiconify / wiconify-source.lzh / Source / wFile.c < prev    next >
C/C++ Source or Header  |  1991-04-19  |  15KB  |  562 lines

  1. /*
  2.  *  WICONIFY    A utility that allows you to iconify any Intuition window
  3.  *              on any screen, and to open WB windows on any screen.
  4.  *
  5.  *  wFile.c     Reads the initialization file.
  6.  *
  7.  *  Copyright 1990 by Davide P. Cervone, all rights reserved.
  8.  *  You may use this code, provided this copyright notice is kept intact.
  9.  */
  10.  
  11. #include "wFile.h"
  12.  
  13. USHORT ImageData[MAXWORDS][MAXHEIGHT][MAXDEPTH];    /* Images are read here */
  14. short MaxWidth,MaxHeight,MaxWords;                  /* Current size of image */
  15. UBYTE Plane0,Plane1;                                /* Bitplanes used */
  16. int ImageType;                                      /* Image, Select, or Mask */
  17. int IconFileOpen;                                   /* TRUE when file is open */
  18.  
  19. static int ScreenCount;             /* How many screens to ignore */
  20. static short ColorID;               /* Which screen color is next */
  21. static short BitMapLine;            /* Which image line is next */
  22. static int NoMenus;                 /* TRUE if no menus defined */
  23.  
  24. extern char **ComName;              /* Current command table */
  25. extern int AutoResize;              /* TRUE if windows are sized to fit */
  26. extern int IconifyPriority,         /* Process priority of handler */
  27.            HandlerPriority;         /* Priority in Input Handler chain */
  28.  
  29.  
  30. /*
  31.  *  ClearBitMap()
  32.  *
  33.  *   Set each word to zero, and clear the dimensions, bitplane counters
  34.  *   and line count.
  35.  */
  36.  
  37. static void ClearBitMap()
  38. {
  39.     short i,j,k;
  40.     
  41.     for (i=0; i<MAXWORDS; i++)
  42.        for (j=0; j<MAXHEIGHT; j++)
  43.           for (k=0; k<MAXDEPTH; k++)
  44.              ImageData[i][j][k] = 0;
  45.     MaxWidth = MaxHeight = MaxWords = 0;
  46.     Plane0 = 0xFF; Plane1 = 0x00;
  47.     BitMapLine = 0;
  48. }
  49.  
  50.  
  51. /*
  52.  *  SetImage()
  53.  *
  54.  *  Set the PlanePick and PlaneOnOff values according to which planes have
  55.  *  zeros and which have ones (if a plane is all zero or all one it can 
  56.  *  use PlaneOnOff and be excluded from PlanePick).
  57.  *
  58.  *  Get a new Image structure (report an error if out of memory)
  59.  *  Count the number of planes of data (the number of 1's in PlanePick).
  60.  *  Set the Image fields.
  61.  *  If there are any planes of data
  62.  *    Allocate enough CHIP memory for them,
  63.  *    If successful,
  64.  *      Set the ImageData pointer,
  65.  *      For each plane in the image,
  66.  *        if the plane is one of the picked planes,
  67.  *          copy the values of the image data for that plane
  68.  *    Otherwise, report a memory error, and free the image structure
  69.  */
  70.  
  71. static void SetImage(theImage)
  72. struct Image **theImage;
  73. {
  74.    short i,j,k;
  75.    UBYTE PlanePick = (~Plane0 & Plane1) | (Plane0 & ~Plane1);
  76.    UBYTE PlaneOnOff = Plane0 & Plane1;
  77.    short Depth = 0;
  78.    UWORD *theWord;
  79.    
  80.    if (NEWSTRUCT(Image,*theImage))
  81.    {
  82.       for (k=PlanePick; k; k>>=1) if (k & 1) Depth++;
  83.       (*theImage)->PlanePick  = PlanePick;
  84.       (*theImage)->PlaneOnOff = PlaneOnOff;
  85.       (*theImage)->Width      = MaxWidth;
  86.       (*theImage)->Height     = MaxHeight;
  87.       (*theImage)->Depth      = Depth;
  88.  
  89.       if (Depth)
  90.       {
  91.          theWord = AllocRaster(MaxWidth,MaxHeight*Depth);
  92.          if (theWord)
  93.          {
  94.             (*theImage)->ImageData = theWord;
  95.             for (k=0; k<MAXDEPTH; k++)
  96.             {
  97.                if (PlanePick & (1<<k))
  98.                {
  99.                   for (j=0; j<MaxHeight; j++)
  100.                      for (i=0; i<MaxWords; i++)
  101.                         *theWord++ = ImageData[i][j][k];
  102.                }
  103.             }
  104.          } else {
  105.             ShowError("Can't get CHIP memory for Image");
  106.             FREESTRUCT(Image,*theImage);
  107.             *theImage = NULL;
  108.          }
  109.       }
  110.    } else ShowError("Can't get memory for Image structure");
  111. }
  112.  
  113.  
  114. /*
  115.  *  StartCommand()
  116.  *
  117.  *  Set up anything needed to begin the specified command.  Usually this
  118.  *  means clear any previous value of the appropriate variables or flags,
  119.  *  and possibly recording the command type for future reference.
  120.  */
  121.  
  122. static void StartCommand(theCommand)
  123. int theCommand;
  124. {
  125.    switch(theCommand)
  126.    {
  127.       case COM_ICONKEY:
  128.          IconifyKey = 0;
  129.          IconifyQuals = IconifyDisquals = 0;
  130.          break;
  131.  
  132.       case COM_CHANGEREFRESH:
  133.          IconifyChange = 0;
  134.          break;
  135.  
  136.       case COM_ACTIVEKEY:
  137.          ActivateKey = 0;
  138.          ActivateQuals = 0;
  139.          break;
  140.  
  141.       case COM_COLORMAP:
  142.          ColorID = 0;
  143.          break;
  144.  
  145.       case COM_IGNORESCREEN:
  146.          ScreenCount = 0;
  147.          break;
  148.  
  149.       case COM_DEFAULTFLAGS:
  150.          DefaultFlags = 0;
  151.          break;
  152.  
  153.       case COM_SCREENFLAGS:
  154.          DefaultScreenFlags = 0;
  155.          break;
  156.  
  157.       case COM_MENUKEYS:
  158.          NoMenus = TRUE;
  159.          break;
  160.  
  161.       case COM_DEFAULTIMAGE:
  162.       case COM_DEFAULTSELECT:
  163.       case COM_DEFAULTMASK:
  164.       case COM_SCREENIMAGE:
  165.       case COM_SCREENSELECT:
  166.       case COM_SCREENMASK:
  167.          ClearBitMap();
  168.          ImageType = theCommand;
  169.          break;
  170.    }
  171. }
  172.  
  173.  
  174. /*
  175.  *  ContinueCommand()
  176.  *
  177.  *  Do the right thing for each kind of command:
  178.  *
  179.  *  ICONKEY:
  180.  *      Read the next part of a key definition allowing qualifiers, 
  181.  *      disqualifiers, and a key name.  If a key name was given, then
  182.  *      it must be the last thing on the line, and the command is over.
  183.  *
  184.  *  CHANGEREFRESH:
  185.  *      Read the next part of a key definition allowing only qualifiers
  186.  *      Keep reading until the next command is read.
  187.  *
  188.  *  ACTIVEKEY:
  189.  *      Read the next part of a key definition allowing qualifiers or a
  190.  *      key name.  If a key name, it must be the last thing on the line,
  191.  *      and the command is complete.
  192.  *
  193.  *  COLORMAP:
  194.  *      Read the next color in the map.
  195.  *
  196.  *  DEFAULTFLAGS:
  197.  *  SCREENFLAGS:
  198.  *      Read the next flag.  Keep reading until the next command is read.
  199.  *
  200.  *  MENUKEYS:
  201.  *      Read the next menu key equivalent, and indicate that menus have
  202.  *      been processed.
  203.  *
  204.  *  IGNORESCREEN:
  205.  *      Get the title of the screen to ignore, and count the screen.
  206.  *      Read additional screen until the next command is read.
  207.  *
  208.  *  PRIORITY:
  209.  *      Get the process priority and check that it is a legal value.
  210.  *      End the command.
  211.  *
  212.  *  HIRESCLICOM:
  213.  *      Get the CLI command string, and end the command.
  214.  *
  215.  *  LORESCLI:
  216.  *      Get the CLI command string, and end the command.
  217.  *
  218.  *  COMMANDSTACK:
  219.  *      Read the stack size for the CLI command, and check that it is
  220.  *      not too small (report an error if it is, and set it to the minimum).
  221.  *      End the command.
  222.  *
  223.  *  IMAGE, SELECT, or MASK:
  224.  *      Read the next line of the image.
  225.  *
  226.  *  DEFAULTICON:
  227.  *  SCREENICON:
  228.  *      Open an Icon file for processing, and end the command (the next
  229.  *      command will come from the file).
  230.  *
  231.  *  OPENON:
  232.  *      Get the OpenOn menu type and end the command.
  233.  *
  234.  *  AUTORESIZE:
  235.  *      Get the resize flag and end the command.
  236.  *
  237.  *  HANDLERPRI:
  238.  *      Get the Input Device handler priority and check that it is valid.
  239.  *      End the command.
  240.  *
  241.  *  If an end-of-line is required but not seen, give an error and skip
  242.  *    to the beginning og the next line.
  243.  *  Return the type of command that we expect next.
  244.  */
  245.  
  246. static int ContinueCommand(theCommand)
  247. int theCommand;
  248. {
  249.    int EndOfLine = TRUE;
  250.  
  251.    switch(theCommand)
  252.    {
  253.       case COM_ICONKEY:
  254.          EndOfLine = ReadKeyDef(&IconifyKey,&IconifyQuals,&IconifyDisquals);
  255.          if (EndOfLine) theCommand = COM_NONE;
  256.          break;
  257.  
  258.       case COM_CHANGEREFRESH:
  259.          ReadKeyDef(NULL,&IconifyChange,NULL);
  260.          EndOfLine = FALSE;
  261.          break;
  262.  
  263.       case COM_ACTIVEKEY:
  264.          EndOfLine = ReadKeyDef(&ActivateKey,&ActivateQuals,NULL);
  265.          if (EndOfLine) theCommand = COM_NONE;
  266.          break;
  267.  
  268.       case COM_COLORMAP:
  269.          ReadColor(ColorID++);
  270.          break;
  271.          
  272.       case COM_DEFAULTFLAGS:
  273.          ReadIconFlags(&DefaultFlags);
  274.          EndOfLine = FALSE;
  275.          break;
  276.  
  277.       case COM_SCREENFLAGS:
  278.          ReadIconFlags(&DefaultScreenFlags);
  279.          EndOfLine = FALSE;
  280.          break;
  281.  
  282.       case COM_MENUKEYS:
  283.          ReadMenuKey(); NoMenus = FALSE;
  284.          break;
  285.  
  286.       case COM_IGNORESCREEN:
  287.          ReadIgnoreScreen();
  288.          ScreenCount++;
  289.          break;
  290.  
  291.       case COM_PRIORITY:
  292.          ReadInteger(&IconifyPriority);
  293.          if (IconifyPriority < -128 || IconifyPriority > 127)
  294.             ShowError("Priority must be in the range -128 to 127"),
  295.             IconifyPriority = 0;
  296.          theCommand = COM_NONE;
  297.          break;
  298.          
  299.       case COM_HIRESCLICOM:
  300.          ReadString(&HiResCLICommand);
  301.          theCommand = COM_NONE;
  302.          break;
  303.  
  304.       case COM_LORESCLICOM:
  305.          ReadString(&LoResCLICommand);
  306.          theCommand = COM_NONE;
  307.          break;
  308.  
  309.       case COM_COMMANDSTACK:
  310.          ReadInteger(&StackSize);
  311.          if (StackSize < 2000)
  312.             ShowError("Stack size must be at least 2K");
  313.             StackSize = 2*1024;
  314.          theCommand = COM_NONE;
  315.          break;
  316.          
  317.       case COM_DEFAULTIMAGE:
  318.       case COM_DEFAULTSELECT:
  319.       case COM_DEFAULTMASK:
  320.       case COM_SCREENIMAGE:
  321.       case COM_SCREENSELECT:
  322.       case COM_SCREENMASK:
  323.          ReadImageLine(BitMapLine++);
  324.          break;
  325.  
  326.       case COM_DEAFULTICON:
  327.          ReadIconFile(COM_DEFAULTIMAGE);
  328.          theCommand = COM_NONE;
  329.          EndOfLine = FALSE;
  330.          break;
  331.  
  332.       case COM_SCREENICON:
  333.          ReadIconFile(COM_SCREENIMAGE);
  334.          theCommand = COM_NONE;
  335.          EndOfLine = FALSE;
  336.          break;
  337.  
  338.       case COM_OPENON:
  339.          ReadOpenOn();
  340.          theCommand = COM_NONE;
  341.          break;
  342.  
  343.       case COM_AUTORESIZE:
  344.          ReadBool(&AutoResize);
  345.          theCommand = COM_NONE;
  346.          break;
  347.  
  348.       case COM_HANDLERPRI:
  349.          ReadInteger(&HandlerPriority);
  350.          if (HandlerPriority < 51 || HandlerPriority > 127)
  351.             ShowError("Priority must be in the range 51 to 127"),
  352.             HandlerPriority = 51;
  353.          theCommand = COM_NONE;
  354.          break;
  355.    }
  356.    if (EndOfLine && NextChar != '\n')
  357.    {
  358.       ReadNextWord();
  359.       ShowError("'%s' seen where End-of-Line was expected",Word);
  360.       SkipLine();
  361.    }
  362.    return(theCommand);
  363. }
  364.  
  365.  
  366. /*
  367.  *  EndCommand()
  368.  *
  369.  *  Finish the command that is in progress, and check that all parameters,
  370.  *  etc, have been included.  If not, give a message about what was
  371.  *  expected to be seen.  For the image commands, copy the image to the
  372.  *  correct image pointer.
  373.  */
  374.  
  375. static void EndCommand(theCommand,newCommand)
  376. int theCommand,newCommand;
  377. {
  378.    char *Wanted = NULL;
  379.  
  380.    switch(theCommand)
  381.    {
  382.       case COM_ICONKEY:
  383.          if (IconifyKey == 0) Wanted = "a KEY or BUTTON definition";
  384.          break;
  385.  
  386.       case COM_ACTIVEKEY:
  387.          if (ActivateKey == 0) Wanted = "a KEY or BUTTON definition";
  388.          break;
  389.  
  390.       case COM_COLORMAP:
  391.          if (ColorID == 0) Wanted = "an RGB Color";
  392.          break;
  393.  
  394.       case COM_MENUKEYS:
  395.          if (NoMenus) Wanted = "a Menu Item Name";
  396.          break;
  397.  
  398.       case COM_IGNORESCREEN:
  399.          if (ScreenCount == 0) Wanted = "a Screen Title";
  400.          break;
  401.  
  402.       case COM_PRIORITY:
  403.       case COM_COMMANDSTACK:
  404.       case COM_HANDLERPRI:
  405.          Wanted = "a Number";
  406.          break;
  407.  
  408.       case COM_HIRESCLICOM:
  409.       case COM_LORESCLICOM:
  410.          Wanted = "a CLI Command";
  411.          break;
  412.  
  413.       case COM_DEFAULTIMAGE:
  414.          if (BitMapLine == 0)
  415.             Wanted = "Image Data";
  416.            else
  417.             SetImage(&DefaultImage);
  418.          break;
  419.  
  420.       case COM_DEFAULTSELECT:
  421.          if (BitMapLine == 0)
  422.             Wanted = "Image Data";
  423.            else
  424.             SetImage(&DefaultSelect);
  425.          break;
  426.  
  427.       case COM_DEFAULTMASK:
  428.          if (BitMapLine == 0)
  429.             Wanted = "Mask Data";
  430.            else
  431.             SetImage(&DefaultMask);
  432.          break;
  433.  
  434.       case COM_SCREENIMAGE:
  435.          if (BitMapLine == 0)
  436.             Wanted = "Image Data";
  437.            else
  438.             SetImage(&DefaultScreenImage);
  439.          break;
  440.  
  441.       case COM_SCREENSELECT:
  442.          if (BitMapLine == 0)
  443.             Wanted = "Image Data";
  444.            else
  445.             SetImage(&DefaultScreenSelect);
  446.          break;
  447.  
  448.       case COM_SCREENMASK:
  449.          if (BitMapLine == 0)
  450.             Wanted = "Mask Data";
  451.            else
  452.             SetImage(&DefaultScreenMask);
  453.          break;
  454.  
  455.       case COM_DEAFULTICON:
  456.       case COM_SCREENICON:
  457.          Wanted = "a File Name";
  458.          break;
  459.  
  460.       case COM_OPENON:
  461.          Wanted = "ACTIVE_SCREEN, CURRENT_WB, or REAL_WB";
  462.          break;
  463.  
  464.       case COM_AUTORESIZE:
  465.          Wanted = "TRUE or FALSE";
  466.          break;
  467.    }
  468.    if (Wanted)
  469.    {
  470.       if (newCommand > COM_LAST) Expected(Wanted); else
  471.          ShowError("Keyword '%s' seen where %s was expected",
  472.             ComName[newCommand],Wanted);
  473.    }
  474. }
  475.  
  476.  
  477. /*
  478.  *  ReadFile()
  479.  *
  480.  *  While there is more data in the file,
  481.  *    Read the next word from the file and interpret it as a command
  482.  *    If the word was a command and there is an old command in progress
  483.  *      End the old command
  484.  *    If the new command is
  485.  *      Not a command:
  486.  *        If there is a command in progress, do it to the current word,
  487.  *        Otherwise give an error and skip to the next line.
  488.  *      Unknown:
  489.  *        Give a message and skip to the next line.
  490.  *      An End-Of-File:
  491.  *        If an icon file is currently being read, close it,
  492.  *        Otherwise set the end-of-file marker to TRUE.
  493.  *      Anything else (i.e., a legitimate command):
  494.  *        Mark the command as the old command, and setup whatever is needed.
  495.  */
  496.  
  497. static void ReadFile(theFile)
  498. APTR theFile;
  499. {
  500.    int PendingCommand = COM_NONE;
  501.    int NewCommand;
  502.    int EOF = FALSE;
  503.  
  504.    while (!EOF)
  505.    {
  506.       NewCommand = ReadCommand();
  507.       if (NewCommand != COM_NONE && PendingCommand != COM_NONE)
  508.          EndCommand(PendingCommand,NewCommand), PendingCommand = COM_NONE;
  509.       switch(NewCommand)
  510.       {
  511.          case COM_NONE:
  512.             if (PendingCommand != COM_NONE)
  513.                PendingCommand = ContinueCommand(PendingCommand);
  514.               else
  515.                Expected("Command Keyword"),
  516.                SkipLine();
  517.             break;
  518.  
  519.          case COM_UNKNOWN:
  520.             ShowError("Unrecognized Command '%s'",Word);
  521.             SkipLine();
  522.             break;
  523.  
  524.          case COM_EOF:
  525.             if (IconFileOpen) EndIconFile();
  526.                else EOF = TRUE;
  527.             break;
  528.  
  529.          default:
  530.             PendingCommand = NewCommand;
  531.             StartCommand(PendingCommand);
  532.             break;
  533.       }
  534.    }
  535. }
  536.  
  537.  
  538. /*
  539.  *  ReadInitFile()
  540.  *
  541.  *  Try to open the primary initialization file,
  542.  *    If unsuccessful, try the secondary file.
  543.  *  If a file was openned, read the file and then close it.
  544.  */
  545.  
  546. void ReadInitFile(Name1,Name2)
  547. char *Name1,*Name2;
  548. {
  549.    if (OpenFile(Name1) == FALSE)
  550.    {
  551.       if (Name2)
  552.          OpenFile(Name2);
  553.         else
  554.          printf("Warning:  Can't open initialization file '%s'\n",Name1);
  555.    }
  556.    if (InFile)
  557.    {
  558.       ReadFile(InFile);
  559.       CloseFile(InFile);
  560.    }
  561. }
  562.